#!/usr/bin/env python

from HTMLParser import HTMLParser

import urllib
class MyParser(HTMLParser):
    def parse(self,s):
        self.feed(s)
        self.close()
    def __init__(self):
        HTMLParser.__init__(self)
        self.results = []
        self.in_table = False
        self.sect_id_content=False
        self.sect_cls_content=False
        self.tbl_cls_list=False
        self.tbd = False
        self.follow=False
        self.span= False
    def handle_starttag(self, tag, attrs):
        if tag=="section" and attrs[0][0] == 'id' and attrs[0][1]=='content':
            self.sect_id_content = True
        elif tag=="section" and attrs[0][0]=='class' and attrs[0][1]=='content':
            self.sect_cls_content = True
        elif tag=="table" and attrs[0][0]=='class' and attrs[0][1]=='list':
            self.tbl_cls_list = True
        elif tag == "tbody":
            self.tbd = True
        elif tag == "a" and attrs[0][0]=='href':
            if self.sect_cls_content and self.sect_id_content and self.tbl_cls_list and self.tbd:
                self.in_table = True #trigger data output flag if all the condition is satisfied.
        elif tag == "td" and len(attrs)!=0:
            if attrs[0][0]=="class" and attrs[0][1]=="nowrap right":
                if self.sect_cls_content and self.sect_id_content and self.tbl_cls_list and self.tbd:
                    self.follow = True
        elif tag == "span" and len(attrs)!=0:
            if attrs[0][0]=="class" and attrs[0][1]=="nb":
                if self.follow == True:
                    self.span=True
    def handle_endtag(self, tag):
        if tag=="section":
            self.sect_cls_content = False
            self.sect_id_content = False
        if tag=="td":
            self.follow = False
            self.span = False
        if tag== "tbody":
            self.tbd = False
        if tag== "table":
            self.tbl_cls_list = False
        if tag=="a":
            self.in_table = False

    def handle_data(self,data):
        if self.in_table==True:
            if len(self.results)>0:
                temp=self.results.pop()
                if not (temp.startswith('@') or data.startswith('@')) and (len(self.results)+1)%4!=0:
                    self.results.append(temp+"&"+data)
                else:
                    self.results.append(temp)
                    self.results.append(data)
            else:
                self.results.append(data)
        if self.follow==True:
            if self.span==False:
                self.results.append(data)
            else:
                self.results.append(self.results.pop()+data);

    def printData(self):
        for value in self.results:
            print value

#put your loop here
pagenum = 2
url = 'http://www.socialbakers.com/twitter/' + 'page-' + str(pagenum)
f = urllib.urlopen(url)
#encoding = f.headers.getparam('charset')
s = f.read()#.decode('utf-8')

# Try and process the page.
# The class should have been defined first, remember.
myparser = MyParser()
myparser.parse(s)
myparser.printData()
